import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/me/face_photo_controller.dart';
import 'package:kkhousekeep/widget/custom_button.dart';
import 'package:kkhousekeep/widget/shadow_widget.dart';
import '../../widget/custom_app_bar.dart';
import '../../widget/dialog/custom_dialog.dart';

class FacePhoto extends StatelessWidget {
  const FacePhoto({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FacePhotoController>(
      init: FacePhotoController(),
      builder: (controller) => Scaffold(
        appBar: CustomAppBar("人脸照片"),
        body: Container(
          padding: EdgeInsets.only(bottom: 60.h),
          color: const Color(0xFFf4f5f6),
          child: SafeArea(
            left: false,
            right: false,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                _buildPhotoView(controller),
                const Spacer(),
                _buildModifyButton(context, controller)
                    .marginOnly(bottom: 24.h),
                _buildSaveButton(controller),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildPhotoView(FacePhotoController controller) {
    return ShadowWidget.fixedHeight(
      radius: 24,
      marginHorizontal: 32,
      marginVertical: 32,
      paddingHorizontal: 46,
      paddingVertical: 32,
      backgroundColor: Colors.white,
      child: Column(
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(12),
            child: Obx(
              () => CachedNetworkImage(
                imageUrl: controller.headPortrait.value,
                fit: BoxFit.fill,
                placeholder: (context, url) => Container(),
                errorWidget: (context, url, error) => Container(),
                width: 420.w,
                height: 588.h,
              ),
            ).marginOnly(bottom: 32.h),
          ),
          Text(
            "注：人脸照片用于身份识别，例如门禁通行等，请规范上传！",
            style: TextStyle(fontSize: 28.sp, color: const Color(0xFF999999)),
          ),
        ],
      ),
    );
  }

  Widget _buildModifyButton(
      BuildContext context, FacePhotoController controller) {
    return CustomButton(
      isAsh: false,
      width: 420.w,
      height: 90.h,
      textColor: const Color(0xFF434649),
      bgColorList: const [Colors.white],
      text: "点击修改",
      onPressed: () {
        Get.bottomSheet(actionSheet(controller));
      },
    );
  }

  Widget _buildSaveButton(FacePhotoController controller) {
    return Obx(()  {
      return CustomButton(
        isAsh: controller.headPortrait.isEmpty,
        width: 420.w,
        height: 90.h,
        text: "保存",
        onPressed: () {
          controller.save();
        },
      );
    });
  }

  // 底部弹出菜单actionSheet
  Widget actionSheet(FacePhotoController controller) {
    return CustomDialog.customCupertinoActionSheet([
      CupertinoActionSheetActionModel("相册选择", () {
        Get.back();
        controller.selectImage();
      }),
      CupertinoActionSheetActionModel("打开摄像头拍照", () {
        Get.back();
        controller.takePhoto();
      }),
    ]);
  }
}
